package com.css.msgcenter.admin.msglog.action;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.dict.service.DictMan;
import com.css.db.page.Page;
import com.css.msgcenter.Results;
import com.css.msgcenter.admin.msglog.service.MsgLogService;
import com.css.msgcenter.server.QueryMsg;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

public class DirLogStatis2 extends AdminAction {
	private static Log log = LogFactory.getLog(DirLogStatis2.class);
	private Date beginTime;
	private Date endTime;
	private String appId, appName;
	private int suc=0,fail=0,al=0;
	private List rlst;
	private String a0,a1,a2,a3,b0,b1,c0,c1,c2,cm;
	private Page page;
	private boolean check = false;
	
	public DirLogStatis2(){
		page = new Page();
	}
	protected String adminGo() {
		if("chart".equals(result)) {
			return result;
		}
		if(check == true) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, -366);
			if((beginTime != null && beginTime.getTime()<=cal.getTime().getTime())
					||(endTime != null && endTime.getTime()<=cal.getTime().getTime())
					||(beginTime != null && endTime != null && beginTime.getTime()>endTime.getTime())) {
				result = Ajax.JSONResult(Results.ERROR, Messages.getString("systemMsg.timeInvalid"));
				return Action.ERROR;
			} else {
				result = Ajax.JSONResult(Results.SUCCESS, Messages.getString("systemMsg.success"));
				return Action.ERROR;
			}
		}
		if(StringHelper.isEmpty(appId))
			return Action.SUCCESS;
		appName = MsgLogService.getAppName(appId);
		StringBuilder sb = new StringBuilder("select a.msg_type as msgtype,") 
		.append("sum(case when(a.msg_status='3') then 1 else 0 end) as suc,")
		.append("sum(case when(a.msg_status='4') then 1 else 0 end) as fail,")
		.append("sum(case when(a.msg_status ='3' or a.msg_status ='4') then 1 else 0 end) as al ")
		.append("from msg_task a left join msg_main b on a.msg_id=b.uuid ")
		.append("where b.app_id =:appId ");
		if(beginTime != null)
			sb.append(" and a.create_time >=:beginTime ");
		if(endTime != null)
			sb.append(" and a.create_time <:endTime ");
		sb.append("group by a.msg_type ");
		if(StringHelper.isNotEmpty(page.getOrderByString())) {
			sb.append(page.getOrderByString());
		}
		QueryMsg qc = new QueryMsg(sb.toString(),true);
		if(StringHelper.isNotEmpty(appId))
			qc.setParameter("appId", appId);
		if(beginTime != null)
			qc.setParameter("beginTime",beginTime);
		if(endTime!= null){
			Calendar cal = Calendar.getInstance();
	        cal.setTime(endTime);
	        cal.add(Calendar.DATE, 1);
	        qc.setParameter("endTime",cal.getTime());
		}
		rlst = qc.list();
		
		if(rlst != null && rlst.size() > 0) {
			for(int i=0; i<rlst.size(); i++){
				Object[] arr = (Object[]) rlst.get(i);
				suc += Integer.valueOf(arr[1].toString());
				fail += Integer.valueOf(arr[2].toString());
				al += Integer.valueOf(arr[3].toString());
			}
		}
		
		StringBuilder a0sb = new StringBuilder();
		StringBuilder a1sb = new StringBuilder();
		StringBuilder a2sb = new StringBuilder();
		StringBuilder b1sb = new StringBuilder();
		if(rlst != null && rlst.size() > 0) {
			for(int i=0; i<rlst.size(); i++){
				Object[] arr = (Object[]) rlst.get(i);
				String msgtype = DictMan.getDictType("msg_d_msgtype", arr[0].toString()).getItemName();
				a0sb.append((i==0?"":",")+"'"+msgtype+"'");
				a1sb.append((i==0?"":",")+Integer.valueOf(arr[1].toString()));
				a2sb.append((i==0?"":",")+Integer.valueOf(arr[2].toString()));
				
				b1sb.append((i==0?"":",")+"{value:"+Integer.valueOf(arr[3].toString())+",name:'"+msgtype+"'}");
			}
		}
		a0 = a0sb.toString();
		a1 = a1sb.toString();
		a2 = a2sb.toString();
		b0 = a0sb.toString();
		b1 = b1sb.toString();
		
		Calendar cal = new GregorianCalendar();
		if(beginTime != null)
			cal.setTime(beginTime);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.MINUTE,0);
		Date beginTime2 = cal.getTime();
		final int DATEN = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.add(GregorianCalendar.MONTH, 1);
		Date endTime2 = cal.getTime();
		
		
//		时间轴图生成
		sb = new StringBuilder()
		.append("select a.msg_type as col1, substring(a.create_time,1,10) as col2, count(*) as col3 from msg_task a ") 
		.append("left join msg_main b on a.msg_id=b.uuid ")
		.append("where b.app_id =:appId ");
		if(beginTime2 != null)
			sb.append(" and a.create_time >=:beginTime ");
		if(endTime2 != null)
			sb.append(" and a.create_time <:endTime ");
		sb.append("group by a.msg_type, substring(a.create_time,1,10) ");
		qc = new QueryMsg(sb.toString(),true);
		if(StringHelper.isNotEmpty(appId))
			qc.setParameter("appId", appId);
		if(beginTime2 != null)
			qc.setParameter("beginTime",beginTime2);
		if(endTime2!= null)
			qc.setParameter("endTime",endTime2);
		List rlst2 = qc.list();
		
		
		HashMap map = new HashMap();
		if(rlst2 != null && rlst2.size() > 0) {
			for(int i=0; i<rlst2.size(); i++){
				Object[] arr = (Object[]) rlst2.get(i);
				String msgtype = DictMan.getDictType("msg_d_msgtype", arr[0].toString()).getItemName();
				if(!map.containsKey(msgtype))
					map.put(msgtype, new HashMap());
				HashMap map2 = (HashMap) map.get(msgtype);
				map2.put(arr[1].toString(), Integer.valueOf(arr[2].toString()));
			}
		}
		
		StringBuilder c2sb = new StringBuilder();
		Iterator ite = map.keySet().iterator();
		while(ite.hasNext()) {
			String k = (String) ite.next();
			c2sb
			.append("{")
			.append("    name:'"+k+"',")
			.append("    type:'line',")
//			.append("    stack: '总量',")
			.append("    data:["+MsgLogService.toStringFunc((HashMap) map.get(k),beginTime2)+"]")
			.append("},");
		}
		if(c2sb.length()>0)
			c2sb.deleteCharAt(c2sb.length()-1);
		else 
			c2sb.append("{}");
		c2 = c2sb.toString();
		
		StringBuilder c0sb = new StringBuilder();
		ite = map.keySet().iterator();
		while(ite.hasNext()) {
			c0sb.append("'"+ite.next()+"',");
		}
		if(c0sb.length()>0)
			c0sb.deleteCharAt(c0sb.length()-1);
		c0 = c0sb.toString();
		
		c1 = c0.length()>0?MsgLogService.getString4Month(beginTime2):"";
		
		cm = new java.text.SimpleDateFormat("yyyy-MM").format(beginTime2);
		
		return Action.SUCCESS;
	}
	
	public static void main(String[] args) {
		DirLogStatis2 a = new DirLogStatis2();
		a.setAppId("e543206d4dcf7b80eae8efa64c07788c");
		a.setResult("chart");
		a.adminGo();
	}
	public Date getBeginTime() {
		return beginTime;
	}
	public String getA0() {
		return a0;
	}
	public void setA0(String a0) {
		this.a0 = a0;
	}
	public String getA1() {
		return a1;
	}
	public void setA1(String a1) {
		this.a1 = a1;
	}
	public String getA2() {
		return a2;
	}
	public void setA2(String a2) {
		this.a2 = a2;
	}
	public String getA3() {
		return a3;
	}
	public void setA3(String a3) {
		this.a3 = a3;
	}
	public String getB0() {
		return b0;
	}
	public void setB0(String b0) {
		this.b0 = b0;
	}
	public String getB1() {
		return b1;
	}
	public void setB1(String b1) {
		this.b1 = b1;
	}
	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	public String getAppName() {
		return appName;
	}
	public void setAppName(String appName) {
		this.appName = appName;
	}
	public int getSuc() {
		return suc;
	}
	public void setSuc(int suc) {
		this.suc = suc;
	}
	public int getFail() {
		return fail;
	}
	public void setFail(int fail) {
		this.fail = fail;
	}
	public int getAl() {
		return al;
	}
	public void setAl(int al) {
		this.al = al;
	}
	public List getRlst() {
		return rlst;
	}
	public void setRlst(List rlst) {
		this.rlst = rlst;
	}
	public String getC0() {
		return c0;
	}
	public void setC0(String c0) {
		this.c0 = c0;
	}
	public String getC1() {
		return c1;
	}
	public void setC1(String c1) {
		this.c1 = c1;
	}
	public String getC2() {
		return c2;
	}
	public void setC2(String c2) {
		this.c2 = c2;
	}
	public String getAppId() {
		return appId;
	}
	public void setAppId(String appId) {
		this.appId = appId;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public String getCm() {
		return cm;
	}
	public void setCm(String cm) {
		this.cm = cm;
	}
	public boolean isCheck() {
		return check;
	}
	public void setCheck(boolean check) {
		this.check = check;
	}
	
}
